package com.centaline.androidsalesblog.widgets.dropdown;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import com.centaline.androidsalesblog.iml.DropListener;
import com.centaline.androidsalesblog.model.businissobject.DropBo;

import java.util.ArrayList;

/**
 * 下拉菜单
 * Created by guilin on 16/1/13.
 */
public abstract class AbsDrop implements PopupWindow.OnDismissListener {

    protected final View anchor;
    protected PopupWindow popupWindow;
    protected ArrayList<DropBo> arrayList = new ArrayList<>();

    protected DropListener localDropListener;
    private DropListener dropListener = new DropListener() {
        @Override
        public void dropComplete(boolean fromMore, int type, DropBo... dropBos) {
            if (localDropListener != null) {
                localDropListener.dropComplete(fromMore, type, dropBos);
            }
        }
    };

    public AbsDrop(View anchor, Context context) {
        this.anchor = anchor;
    }

    protected void initPopWindow(View view) {
        popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT, true);
        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        popupWindow.setOnDismissListener(this);
    }

    /**
     * 初始化
     */
    public abstract void init(ArrayList<DropBo> arrayList);

    public void setDropListener(DropListener localDropListener) {
        this.localDropListener = localDropListener;
    }

    /**
     * @param fromMore 是否来源更多
     * @param type     下拉菜单类型
     * @param dropBos  数据
     */
    protected void dropComplete(boolean fromMore, int type, DropBo... dropBos) {
        dropListener.dropComplete(fromMore, type, dropBos);
        dismiss();
    }

    /**
     * 显示
     */
    public void show() {
        if (popupWindow != null &&
                !popupWindow.isShowing())
            popupWindow.showAsDropDown(anchor);
    }

    protected void dismiss() {
        if (popupWindow != null)
            popupWindow.dismiss();
    }
}
